package com.cskaoyan.market.controller;

import com.cskaoyan.market.service.MarketConfigMallService;
import com.cskaoyan.market.service.impl.MarketConfigMallServiceImpl;
import com.cskaoyan.market.util.HttpUtil;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;

@WebServlet("/admin/config/*")
public class AdminConfigController extends HttpServlet {
    MarketConfigMallService service=new MarketConfigMallServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String op = req.getRequestURI().replace(req.getContextPath() + "/admin/config/", "");
        if("mall".equals(op)){
            mall(req,resp);
        }
        if("express".equals(op)){
            express(req,resp);
        }
        if("order".equals(op)){
            order(req,resp);
        }
        if("wx".equals(op)){
            wx(req,resp);
        }
    }
    private void mall(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        HashMap<String, String> data = service.getConfigMall();
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.ok(data)));
    }
    private void express(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        HashMap<String, String> data = service.getConfigExpress();
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.ok(data)));
    }
    private void order(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        HashMap<String, String> data = service.getConfigOrder();
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.ok(data)));
    }
    private void wx(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        HashMap<String, String> data = service.getConfigWx();
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.ok(data)));
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String op = req.getRequestURI().replace(req.getContextPath() + "/admin/config/", "");
        if("mall".equals(op)){
            mallP(req,resp);
        }
        if("express".equals(op)){
            expressP(req,resp);
        }
        if("order".equals(op)){
            orderP(req,resp);
        }
        if("wx".equals(op)){
            wxP(req,resp);
        }
    }
    private void mallP(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = HttpUtil.getRequestBody(req);
        String mall_address = JacksonUtil.parseString(requestBody, "market_mall_address");
        String market_mall_latitude = JacksonUtil.parseString(requestBody, "market_mall_latitude");
        String market_mall_longitude = JacksonUtil.parseString(requestBody, "market_mall_longitude");
        String market_mall_name = JacksonUtil.parseString(requestBody, "market_mall_name");
        String market_mall_phone = JacksonUtil.parseString(requestBody, "market_mall_phone");
        String market_mall_qq = JacksonUtil.parseString(requestBody, "market_mall_qq");
        service.updataMall(mall_address,market_mall_latitude,market_mall_longitude,market_mall_name,market_mall_phone,market_mall_qq);
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.ok()));
    }
    private void expressP(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = HttpUtil.getRequestBody(req);
        String marketExpressFreightMin = JacksonUtil.parseString(requestBody, "market_express_freight_min");
        String marketExpressFreightValue = JacksonUtil.parseString(requestBody, "market_express_freight_value");
        service.updateExp(marketExpressFreightValue,marketExpressFreightMin);
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.ok()));
    }
    private void orderP(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = HttpUtil.getRequestBody(req);
        String marketOrderComment = JacksonUtil.parseString(requestBody, "market_order_comment");
        String marketOrderUnconfirm = JacksonUtil.parseString(requestBody, "market_order_unconfirm");
        String marketOrderUnpaid = JacksonUtil.parseString(requestBody, "market_order_unpaid");
        service.upOrder(marketOrderUnpaid,marketOrderUnconfirm,marketOrderComment);
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.ok()));
    }
    private void wxP(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = HttpUtil.getRequestBody(req);
        String marketWxCatlogGoods = JacksonUtil.parseString(requestBody, "market_wx_catlog_goods");
        String marketWxCatlogList = JacksonUtil.parseString(requestBody, "market_wx_catlog_list");
        String marketWxIndexBrand = JacksonUtil.parseString(requestBody, "market_wx_index_brand");
        String marketWxIndexHot = JacksonUtil.parseString(requestBody, "market_wx_index_hot");
        String marketWxIndexNew = JacksonUtil.parseString(requestBody, "market_wx_index_new");
        String marketWxIndexTopic = JacksonUtil.parseString(requestBody, "market_wx_index_topic");
        String marketWxShare = JacksonUtil.parseString(requestBody, "market_wx_share");
        service.updateWx(marketWxShare,marketWxCatlogGoods,marketWxCatlogList,marketWxIndexBrand,marketWxIndexHot,marketWxIndexNew,marketWxIndexTopic);
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.ok()));
    }
}
